package com.go.manaservice.controller;

import java.util.*;

import com.go.manaservice.entity.TblManagement;
import com.go.manaservice.entity.vo.ManagmentQuery;
import com.go.manaservice.service.ManagementService;
import com.go.serviceutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhengYuXiang
 * @version 1.0
 * @date 2021/11/25 12:49 上午
 */
@CrossOrigin
@Api(description = "系统管理人员管理")
//把这个类交给spring管理，并且要返回数据，会自己转换为json
@RestController
@RequestMapping("/manaservice/managemnt")
public class ManagementController {
    @Autowired
    private ManagementService managementService;

    //rest风格每个动作对应不同的请求,访问地址加上面的，有无/都无关系
    // @ApiOperation这是swagger的注解，里面就相当于方法解释或者参数名解释
    @ApiOperation(value = "查询所有管理者")
    @GetMapping("findAll")
    public R findAllManagement() {
        ManagmentQuery managmentQuery = new ManagmentQuery();
//查询所有用户
        List<TblManagement> tblManagements = managementService.list(managmentQuery);

        return R.ok().data("items", tblManagements);
    }

    @ApiOperation(value = "登录验证")
    @GetMapping("findOne/{account}/{password}")
    public R findOneManagement(@ApiParam(value = "管理人员账号")
                               @PathVariable String account,
                               @ApiParam(value = "管理人员密码")
                               @PathVariable String password) {

        System.out.println(account + password);
        ManagmentQuery managmentQuery = new ManagmentQuery();
        managmentQuery.setAccount(account);
        managmentQuery.setPassword(password);

        //查询符合账号和密码的用户
        List<TblManagement> tblManagements = managementService.list(managmentQuery);
        if (tblManagements.size() > 0) {
            return R.ok();
        } else {
            R.error();
        }
        return R.error();
    }


    //    {id}表示要通过路径传参数，
    @ApiOperation(value = "删除某一个具体的管理人员")
    @DeleteMapping("{id}")
//    @PathVariable int id 就是上面的id
    public R removeManagement(
            @ApiParam(value = "管理人员ID")
            @PathVariable int id) {
        Map<String, Object> map = new HashMap();
        map.put("id", id);
        if (managementService.deleteManagementS(map)) {
            return R.ok();
        } else {
            R.error();
        }
        return R.error();
    }

    @ApiOperation(value = "批量删除某一个具体的管理人员")
    @DeleteMapping("deleteMany")
//    @PathVariable int id 就是上面的id
    public R removeManyManagement(
            @ApiParam(value = "管理人员ID数组")
            @RequestParam(value ="array[]") String[] ids) {

//      ArrayList delete = new ArrayList<Integer>();

//        map.put("id", id);
//        if (managementService.deleteManagementS(map)) {
//            return R.ok();
//        } else {
//            R.error();
//        }

        return R.error();
    }

    @ApiOperation(value = "分页获取管理人员信息")
    @PostMapping("pageManagement/{current}/{limit}")
    public R pageListManagement(@ApiParam(value = "每页数量")
                                @PathVariable String limit,
                                @ApiParam(value = "当前页")
                                @PathVariable String current,
                                @ApiParam(value = "查询对象条件")
                                @RequestBody(required = false) ManagmentQuery managmentQuery) {
        Integer limit1 = Integer.valueOf(limit);
        Integer current1 = Integer.valueOf(current);
        System.out.println(12123);
       managmentQuery.setCurrent((current1 - 1) * limit1);
       managmentQuery.setLimit(limit1);
//       带条件的查询用户信息
        List<TblManagement> list = managementService.list(managmentQuery);
        //带条件的查询用户数量
        int i = managementService.selectManaCount(managmentQuery);
        return R.ok().data("total", i).data("rows", list);
    }

    //@RequestBody前端参数存在对应的对象里面
    @ApiOperation(value = "添加管理人员信息")
    @PostMapping("addManagement")
    public R addManagement(@RequestBody TblManagement tblManagement) {
        boolean b = managementService.addManagement(tblManagement);
        if (b) {
            return R.ok();
//            写完if都写else为了保持思维习惯上的严谨
        } else {
            return R.error();
        }

    }

    @ApiOperation(value = "修改管理人员信息")
    @PostMapping("updataManagement")
    public R updateManagement(@RequestBody TblManagement tblManagement) {
        boolean b = managementService.updateManagement(tblManagement);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }

    }
    @ApiOperation(value = "根据ID查询管理人员信息")
    @GetMapping("findOne/{id}")
    public R findManagementOne(@PathVariable Integer id) {
        TblManagement managementOne = managementService.findManagementOne(id);
        if (managementOne!=null){
            return R.ok().data("oneData",managementOne);
        }
      else {
            return R.error();
        }

    }

}

